package com.lt.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lucine
 * @version 1.0 2022/4/16 17:30
 */
@Component
public class DelayedQueueConfig {

    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";

    public static final String DELAYED_ROUTINGKEY_NAME = "delayed.routingKey";

    public static final String DELAYED_QUEUE_NAME = "delayed.queue";


    /**
     * 延时交换机
     *
     * @return TopicExchange
     */
    @Bean
    public TopicExchange delayExchange() {
        Map<String, Object> pros = new HashMap<>(3);
        //设置交换机支持延迟消息推送
        pros.put("x-delayed-message", "topic");
        TopicExchange exchange = new TopicExchange(DELAYED_EXCHANGE_NAME, true, false, pros);
        //我们在也可以在 Exchange 的声明中可以设置exchange.setDelayed(true)来开启延迟队列
        exchange.setDelayed(true);
        return exchange;
    }
    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue delayedQueue(){
        return new Queue(DELAYED_QUEUE_NAME);
    }

    @Bean
    public Binding delayBing(@Qualifier("delayedQueue") Queue queue,
                             @Qualifier("delayExchange") TopicExchange TopicExchange){
        return BindingBuilder.bind(queue).to(TopicExchange).with(DELAYED_ROUTINGKEY_NAME);
    }

}
